<?php

namespace App\Http\Controllers\Wx;

use App\CodeResponse;
use App\Exceptions\BusinessException;
use App\Inputs\GoodsListInput;
use App\Services\CollectServices;
use App\Services\CommentService;
use App\Services\CommentServices;
use App\Services\Goods\BrandServices;
use App\Services\Goods\CatalogServices;
use App\Services\Goods\GoodsServices;
use App\Services\Goods\SearchHistoryServices;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class GoodsController extends WxController
{
    protected $only = [];

    /**
     * 获取商品数量
     * @return JsonResponse
     */
    public function count()
    {
        $count = GoodsServices::getInstance()->countGoodsOnSale();
        return $this->success($count);
    }

    /**
     * @param  Request  $request
     *
     * @return JsonResponse
     * @throws BusinessException
     */
    public function category(Request $request): JsonResponse
    {
        $id = $this->verifyId('id');
        $cur = CatalogServices::getInstance()->getCategory($id);
        if (empty($cur)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $parent = null;
        $children = null;
        if ($cur->pid == 0) {
            $parent = $cur;
            $children = CatalogServices::getInstance()->getL2ListByPid($cur->id);
            $cur = $cur->first() ?? $cur;
        } else {
            $parent = CatalogServices::getInstance()->getL1ById($cur->pid);
            $children = CatalogServices::getInstance()->getL2ListByPid($cur->pid);
        }
        return $this->success([
            'currentCategory' => $cur,
            'parentCategory' => $parent,
            'brotherCategory' => $children
        ]);
    }
    public function list(Request $request){
        /*$categoryId = $request->input('categoryId');
        $brandId = $request->input('brandId');
        $keyword = $request->input('keyword');
        $isNew = $request->input('isNuew');
        $isHot = $request->input('isHot');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','add_time');
        $order = $request->input('order','desc');*/
        // 对 request 的请求参数进行验证
        // 1
        /*$input = $request->validate(
            [
                'categoryId' => 'integer|digits_between:1,20',
                'brandId' => 'integer|digits_between:1,20',
                'keyword' => 'string',
                'isNew' => 'boolean',
                'isHot' => 'boolean',
                'page' => 'integer',
                'limit' => 'integer',
                'sort' => Rule::in(['add_time','retail_price','name']),
                'order' => Rule::in(['desc','asc']),
            ]
        );

        $categoryId = $input['categoryId'] ?? 0;*/
        //2
        //$categoryId = $this->verifyId('categoryId');
        // 3
        /*$categoryId = $this->verifyId('categoryId');
        $brandId = $this->verifyId('brandId');
        $keyword = $this->verifyString('keyword');
        $isNew = $this->verifyBoolean('isNew');
        $isHot = $this->verifyBoolean('isHot');
        $page = $this->verifyInteger('page',1);
        $limit = $this->verifyInteger('limit',10);
        $sort = $this->verifyEnum('sort','add_time',['add_time','retail_price']);
        $order = $this->verifyEnum('order','desc',['desc','asc']);*/
        // 4 优化成对象
        /*$input = new GoodsListInput();
        $input = $input->fill();*/
        //$input = GoodsListInput::new([\request()->input()]);
        $input = GoodsListInput::new();

        // 保存搜索记录
        if($this->isLogin() && !empty($keyword)){
            SearchHistoryServices::getInstance()->save($this->userId(),$keyword,Constant::SEARCH_HISTORY_FROM_WX);
        }
        $columns = ['id','name','brief','pic_url','is_new','is_hot','counter_price','retail_price'];
        /* $goodsList = GoodsServices::getInstance()->listGoods($categoryId,$brandId,$isNew,$isHot,$keyword,$sort,$order,$page,$limit,$columns);
         */
        $goodsList = GoodsServices::getInstance()->listGoods($input,$columns);
        //$categoryList = GoodsServices::getInstance()->listL2Category($brandId,$isNew,$isHot,$keyword);
        $categoryList = GoodsServices::getInstance()->listL2Category($input);
        $goodsList = $this->paginate($goodsList);
        $goodsList['filterCategoryList'] = $categoryList;
        return $this->success($goodsList);

    }

    /**
     * @param  Request  $request
     *
     * @return JsonResponse
     * @throws BusinessException
     */
    public function detail(Request $request): JsonResponse
    {
        $id = $this->verifyId('id');
        $goods = GoodsServices::getInstance()->getGoods($id);
        if(empty($goods)){
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        // 获取商品属性
        $attr = GoodsServices::getInstance()->getGoodsAttribute($id);
        //获取商品sku规格
        $spec = GoodsServices::getInstance()->getGoodsSpecification($id);
        // 获取sku商品详情
        $product = GoodsServices::getInstance()->getGoodsProduct($id);
        //Issue 问题 列表
        $issue = GoodsServices::getInstance()->getGoodsIssue();
        // 商品详情
        $brand = $goods->brand_id ? BrandServices::getInstance()->getBrand($goods->brand_id) : (object)[];
        // 获取商品评论
        $comment = CommentServices::getInstance()->getCommentWithUserInfo($id);
        //用户收藏数量
        $userHasCollect = 0;
        if($this->isLogin()){
            $userHasCollect = CollectServices::getInstance()->countByGoodsId($this->userId(),$id);
            //保存用户足迹
            GoodsServices::getInstance()->saveFootPrint($this->userId(),$id);
        }
        return $this->success([
            'info' => $goods,
            'userHasCollect' => $userHasCollect,
            'issue' => $issue,
            'comment' => $comment,
            'specificationList' => $spec,
            'productList' => $product,
            'attribute' => $attr,
            'brand' => $brand,
            'groupon' => [],
            'share' => false,
            'shareImage' => $goods->share_url,
        ]);
    }



}
